package com.towexin.application.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.towexin.common.ResultData;
import com.towexin.config.mylogging.LogType;
import com.towexin.config.mylogging.MyLog;
import com.towexin.application.system.domain.Role;
import com.towexin.application.system.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author 唐伟鑫
 * @version 1.0
 * @date 2021/4/13 15:12
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService service;

    @MyLog(type = LogType.INSERT, operation = "添加角色")
    @RequiresPermissions("system.role.add")
    @PostMapping("save")
    public ResultData save(Role role){
        if (service.getOne(new QueryWrapper<Role>().eq("mark", role.getMark()))!=null)
            return ResultData.NO("角色标识重复");
        return service.saveOrUpdateOne(role)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.UPDATE, operation = "修改角色信息")
    @RequiresPermissions("system.role.edit")
    @PostMapping("update")
    public ResultData update(Role role){
        if (service.getOne(new QueryWrapper<Role>().eq("mark", role.getMark()).ne("id", role.getId()))!=null)
            return ResultData.NO("角色标识重复");
        return service.saveOrUpdateOne(role)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.DELETE, operation = "删除角色")
    @GetMapping("deleteOne")
    @RequiresPermissions("system.role.delete")
    public ResultData deleteOne(String id){
        return service.deleteOneById(id)?ResultData.OK():ResultData.NO();
    }

    //@MyLog(type = 1, operation = "获取角色列表")
    @GetMapping("selectAll")
    @RequiresPermissions("system.role.list")
    public ResultData selectPage(){
        return ResultData.OK(service.selectAllByUserName());
    }

    @GetMapping("selectRoleTree")
    public ResultData selectRoleTree(){
        return ResultData.OK(service.selectRoleTree(true));
    }

    @GetMapping("selectUserRoleTree")
    public ResultData selectUserRoleTree(){
        return ResultData.OK(service.selectRoleTree(false));
    }

    @MyLog(type = LogType.UPDATE, operation = "设置角色权限")
    @PostMapping("authorization")
    @RequiresPermissions("system.role.authorization")
    public ResultData authorization(String rId, String authIdJson){
        return service.roleAuthorization(rId, authIdJson)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.UPDATE, operation = "分配用户默认权限")
    @GetMapping("set-default")
    @RequiresPermissions("system.role.default")
    public ResultData authorization(String id, boolean defaultRole){
        return service.setDefaultRole(id, defaultRole)?ResultData.OK():ResultData.NO();
    }

    @GetMapping("changeStatus")
    @MyLog(type = LogType.UPDATE, operation = "设置状态")
    @RequiresPermissions("system.role.status")
    public ResultData changeRoleStatus(String id, Boolean status){
        return service.changeStatus(id, status)?ResultData.OK():ResultData.NO();
    }

    @GetMapping("getAuthority")
    public ResultData getAuthority(String id){
        return ResultData.OK(service.getAuthorityByRid(id));
    }

}
